using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using TimeWar;

#if PC
using System.Diagnostics;
#endif

namespace TimeWar
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class SideSelection : Screen
    {

        private int screenheight, screenwidth;

        private GameCulture timeLordCulture, dalekCulture;

        private Game gameObj;

        private Cue menuMusic;

        private GameCursor cursorObj = null;

        private Cultures selectedCulture;

        protected SpriteBatch spriteBatch = null;

        public Cultures SelectedCulture
        {
            get
            {
                return selectedCulture;
            }
        }

        public SideSelection(Game game)
        {

            gameObj = game;

#if PC
            Trace.WriteLine("[" + DateTime.Now + "]: SideSelection constructor called.");            

#endif
            // TODO: Construct any child components here
            //Start menu music
            //menuMusic = Sound.Play(Sounds.MenuMusic);

            LoadContent();

        }

        public GameCursor Cursor
        {
            get
            {
                return cursorObj;
            }
        }

        protected void LoadContent()
        {

#if PC
            Trace.WriteLine("[" + DateTime.Now + "]: In SideSelection.LoadContent...");
            Trace.WriteLine("[" + DateTime.Now + "]: ContentManager Root Directory: " + TimeWarGame.ContentManager.RootDirectory);
            Trace.Flush();
#endif

            spriteBatch = new SpriteBatch(gameObj.GraphicsDevice);

            screenheight = 720;
            screenwidth = 1280;

            //timeLordCulture = new GameCulture(gameObj, spriteBatch, 0, 0, Cultures.TimeLords, "Content\\Textures\\ChooseTimeLords", "Content\\Fonts\\CourierNew", screenwidth, screenheight);
            timeLordCulture = new GameCulture(gameObj, spriteBatch, 0, 0, Cultures.TimeLords, @"Textures\\ChooseTimeLords", @"Fonts\\CourierNew", screenwidth, screenheight);
 
            //dalekCulture = new GameCulture(gameObj, spriteBatch, -1, 0, Cultures.Daleks, "Content\\Textures\\ChooseDaleks", "Content\\Fonts\\CourierNew", screenwidth, screenheight);
            dalekCulture = new GameCulture(gameObj, spriteBatch, -1, 0, Cultures.Daleks, @"Textures\\ChooseDaleks", @"Fonts\\CourierNew", screenwidth, screenheight);

            cursorObj = new GameCursor(spriteBatch);

            timeLordCulture.Cursor = cursorObj;
            dalekCulture.Cursor = cursorObj;


        }

        public override void Render()
        {
            IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)gameObj.Services.GetService(typeof(IGraphicsDeviceService));

            GraphicsDevice device = graphicsService.GraphicsDevice;

            //spriteBatch = new SpriteBatch(device);

            //Sprites will always be at the back.
            device.RenderState.DepthBufferWriteEnable = false;
            

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None);

            //Drawing code fits in here.
            timeLordCulture.Render();
            dalekCulture.Render();
            cursorObj.Render();
            



            //Draw tardis cursor at mouse position...game position



            spriteBatch.End();

        }

        public override GameState Update(TimeSpan time, TimeSpan elapsedTime)
        {
            //XInputHelper.GamePads.Update(gameObj, new KeyboardState());
            XInputHelper.GamePads[PlayerIndex.One].Update(gameObj, new KeyboardState());

            GameTime timeWarTime = new GameTime(time, elapsedTime, time, elapsedTime);
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Texture, SaveStateMode.None);

            if (dalekCulture.isOver(cursorObj.Position) && timeLordCulture.isOver(cursorObj.Position) == false)
            {
                cursorObj.IsOverDaleks = true;
                cursorObj.IsOverTimeLords = false;
            }
            else if (timeLordCulture.isOver(cursorObj.Position) && dalekCulture.isOver(cursorObj.Position) == false)
            {
                cursorObj.IsOverDaleks = false;
                cursorObj.IsOverTimeLords = true;
            }
            else
            {
                cursorObj.IsOverDaleks = false;
                cursorObj.IsOverTimeLords = false;
            }

            dalekCulture.Render();
            timeLordCulture.Render();
            cursorObj.Render();

#if PC
            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {

                nextState = GameState.TeamSelection;

                if (cursorObj.IsOverDaleks == true && cursorObj.IsOverTimeLords == false)
                {
                    selectedCulture = Cultures.Daleks;
                    Trace.WriteLine("[" + DateTime.Now + "]: SideSelection:Update(): Dalek culture selected.");

                }
                else if (cursorObj.IsOverDaleks == false && cursorObj.IsOverTimeLords == true)
                {
                    selectedCulture = Cultures.TimeLords;
                    Trace.WriteLine("[" + DateTime.Now + "]: SideSelection:Update(): Time Lord culture selected.");
                }
                else
                {

                }
            }


#endif

            if (XInputHelper.GamePads[PlayerIndex.One].APressed)
            {


                if (cursorObj.IsOverDaleks == true && cursorObj.IsOverTimeLords == false)
                {
                    selectedCulture = Cultures.Daleks;
#if PC
                    Trace.WriteLine("[" + DateTime.Now + "]: SideSelection:Update(): Dalek culture selected.");
                    
#endif

                    return GameState.TeamSelection;

                }
                else if (cursorObj.IsOverDaleks == false && cursorObj.IsOverTimeLords == true)
                {
                    selectedCulture = Cultures.TimeLords;
#if PC
                    Trace.WriteLine("[" + DateTime.Now + "]: SideSelection:Update(): Time Lord culture selected.");
#endif
                    return GameState.TeamSelection;
                }
                else
                {

                }
            }

            spriteBatch.End();
            //return base.Update(time, elapsedTime);
            return GameState.SideSelection;


        }

        public override void Shutdown()
        {
            if (spriteBatch != null)
            {
                spriteBatch.End();
                spriteBatch.Dispose();
                spriteBatch = null;
            }
        }

     



    }
}